import VueRouter from 'vue-router';
import meun from './meun';
// 单独页面开始
const Login = () => import(/* webpackChunkName: "login" */ 'components/login/index');
const NotFound = () => import(/* webpackChunkName: "404" */ 'components/404/index');
const Console = () => import(/* webpackChunkName: "console" */ 'components/console');
const commonUser = () => import(/* webpackChunkName: "console" */ 'components/general/commonUser');
// 单独页面结束
//配置管理开始
const configuration_application_application_management = () => import('components/configuration/application/applicationManagement');
const configuration_operation_operation_statistics = () => import('components/configuration/operation/operationStatistics');
const configuration_system_system_set = () => import('components/configuration/system/systemSet');
const configuration_user_user_management = () => import('components/configuration/user/userManagement');
//配置管理结束
const router = new VueRouter({
  mode: 'hash',
  routes: [
    { path: '/', redirect: '/console' },
    {
      path: '/login',
      name: 'login',
      meta: { noLogin: true },
      component: Login,
      // 单独配置路由守卫，清除http请求内部的token
      beforeEnter: (to, from, next) => {
        localStorage.removeItem('TOKEN');
        return next();
      }
    },
    {
      path: '/console',
      component: Console,
      children: [
        // { path: '', component: configuration_admin_manage }, //默认使用哪一个路由
        { path: 'configuration/application/applicationManagement', component: configuration_application_application_management },
        { path: 'configuration/operation/operationStatistics', component: configuration_operation_operation_statistics},
        { path: 'configuration/system/systemSet', component: configuration_system_system_set},
        { path: 'configuration/user/userManagement', component: configuration_user_user_management},
      ]
    },
    {
      path:'/common_user',
      component: commonUser,
    },
    { path: '*', component: NotFound }
  ]
});
// 策略模式，对重定向的路由标题进行修复
const redirect = {
  '/console': ''
};
const special = {
  '/login': '登录',
  '/common_user': '用户管理'
};
// 添加后置路由
router.afterEach((to) => {
  let { path } = to;
  const title = special[path];
  if (title) {
    document.title = title;
    return;
  }
  path = redirect[path] || path;
  // 简单深拷贝一下数组，递归寻找一下标题
  const arr = meun();
  while (arr.length) {
    const child = arr.pop();
    if (path === child.name) {
      document.title = child.text;
      break;
    }
    if (Array.isArray(child.children)) {
      arr.push(...child.children);
    }
  }
});
router.beforeEach((to, from, next) => { //路由拦截,重定向
  // 1. 判断访问的是不是登录 页面
  if (to.path === '/login') {
    next()
  } else {
    if(localStorage.getItem('TOKEN')){
      //next()
      if(to.path === '/common_user'&&JSON.parse(localStorage.getItem('userInfo')).type===0){
        next()
      }else if(to.path !== '/common_user'&&JSON.parse(localStorage.getItem('userInfo')).type===-1){
        next()
      }else {
        next('/login')
      }
    }else{
      next('/login')
    }
  }
})
export default router;
